import {REWARD_TYPE} from "../../engines/services/userContent/userInterface";

const {ccclass, property} = cc._decorator;

@ccclass
export default class RewardView extends cc.Component {

    @property(cc.Node)
    BgLight: cc.Node = null;
    @property(cc.Label)
    lbTitle: cc.Label = null;
    @property(cc.Label)
    lbCount: cc.Label = null;
    @property(cc.Sprite)
    spRewardIcon: cc.Sprite = null;
    @property(cc.SpriteFrame)
    spfPower: cc.SpriteFrame = null;
    @property(cc.SpriteFrame)
    spfCoin: cc.SpriteFrame = null;
    @property(cc.SpriteFrame)
    spfDiamond: cc.SpriteFrame = null;

    protected onLoad(): void {
        this.spRewardIcon.spriteFrame = null;
        this.lbTitle.string = '恭喜';
        //
        this.OnIn();
        this.node.zIndex = cc.macro.MAX_ZINDEX;
    }

    OnShowReward(rewardType: REWARD_TYPE, rewardCount: number, titleString: string = '恭喜', customSpriteFrame?: cc.SpriteFrame) {
        this.lbTitle.string = titleString;
        this.lbCount.string = `+${Math.floor(rewardCount)}`;
        //
        switch (rewardType) {
            case REWARD_TYPE.POWER: {
                this.spRewardIcon.spriteFrame = this.spfPower;
                break;
            }
            case REWARD_TYPE.COIN: {
                this.spRewardIcon.spriteFrame = this.spfCoin;
                break;
            }
            case REWARD_TYPE.DIAMOND: {
                this.spRewardIcon.spriteFrame = this.spfDiamond;
                break;
            }
            case REWARD_TYPE.CUSTOM: {
                this.spRewardIcon.spriteFrame = customSpriteFrame;
                break;
            }
            default: {
                console.error('错误的奖励类型', rewardType);
                break;
            }
        }
    }

    protected OnRewardBg() {
        //
        cc.tween(this.BgLight)
            .by(1, {angle: 120})
            .repeatForever()
            .start();
    }

    protected OnBtnKnow() {
        this.OnOut();
    }

    protected OnIn() {
        this.node.scale = 0.1;
        this.OnRewardBg();
        cc.tween(this.node)
            .to(0.3, {scale: 1}, {easing: 'backOut'})
            .call(() => {
                // this.OnRewardBg();
            })
            .start();
    }

    protected OnOut() {
        cc.tween(this.node).to(0.3, {scale: 0.1}, {easing: "backIn"}).call(() => {
            this.node.removeFromParent()
            this.node.destroy()
        }).start()
    }
}
